package anonymouschat.service;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

import anonymouschat.messages.Message;
import anonymouschat.messages.TextMessage;
import anonymouschat.messages.serverresponses.ChatroomChangeResponse;
import anonymouschat.messages.serverresponses.ChatroomMemberResponse;


/**
 * Connection leaving a server (send messages FROM server with an instance of
 * this class)
 * 
 */
public class OutConnection
{
    private Socket clientSocket;

    private String name;

    private ObjectOutputStream out;

    /**
     * Create Instance with the socket connection clientSocketIn, and announcing
     * who is connected with messageIn
     * 
     * @param clientSocketIn
     *            - Socket connecting to CLIENT
     * @param messageIn
     *            - ????
     */
    OutConnection( Socket clientSocketIn, String userNameIn ) {
        clientSocket = clientSocketIn;
        name = userNameIn;
        try {
            out = new ObjectOutputStream( clientSocket.getOutputStream() );
        } catch( IOException e ) {
            e.printStackTrace();
        }

    }

    public String getName() {
        return name;
    }

    public ObjectOutputStream getOutputStream() {
        return this.out;
    }

    /**
     * Send message using this OutConnection's Socket
     * 
     * @param message
     *            - message to send
     * @throws IOException
     */
    public void sendMessage( Message message ) throws IOException {
        out.writeObject( message );
        out.flush();
    }

}
